package indi.zhifa.engine.core.excel.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;


public class ExcelUtils {
    public static String getCol(int pCol){
        Stack<Integer> st = new Stack<>();
        while (pCol>=0){
            st.push(pCol%26);
            pCol = pCol / 26 - 1;
        }
        StringBuilder sb = new StringBuilder();
        while (!st.isEmpty()){
            sb.append((char)(st.pop() + 'A'));
        }
        return sb.toString();
    }

    public static String encodeURIComponent(String input,boolean pErase) {
        Map<Integer,String> rpMap = new HashMap<Integer,String>(){{
            put('+'-0,"%2B");
            put(' '-0,"%20");
            put('/'-0,"%2F");
            put('?'-0,"%3F");
            put('%'-0,"%25");
            put('#'-0,"%23");
            put('&'-0,"%26");
            put('='-0,"%3D");
        }};
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<input.length();i++){
            char c = input.charAt(i);
            if(rpMap.containsKey(c-0)){
                if(!pErase){
                    sb.append(rpMap.get(c-0));
                }
            }else{
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static Integer byte2int(byte[] bytes){
        if(null == bytes){
            return null;
        }
        int values = 0;
        int len = Math.min(bytes.length,4);
        for (int i = 0; i < len; i++) {
            values <<= 8;
            values |= (bytes[i] & 0xff);
        }
        return values;
    }

    public static Long byte2Long(byte[] bytes){
        if(null == bytes){
            return null;
        }
        long values = 0;
        int len = Math.min(bytes.length,8);
        for (int i = 0; i < len; i++) {
            values <<= 8;
            values |= (bytes[i] & 0xff);
        }
        return values;
    }

    public static String expandUrl(String pUrl, Map<String, Object> pParam)
    {
        StringBuilder sb = new StringBuilder();
        sb.append(pUrl);
        boolean hasParam = pUrl.indexOf("?") > 0;
        if(pParam.size() > 0 && !hasParam){
            sb.append("?");
        }
        boolean first = true;
        for (Map.Entry<String,Object> paramDefine : pParam.entrySet()) {
            if(!first){
                sb.append("&");
            }
            sb.append(paramDefine.getKey()+"="+paramDefine.getValue());
            first = false;
        }
        return sb.toString();
    }
}
